//
//  SLDownloadFileModel.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/7.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLDownloadFileModel.h"

#import "SLDownloadDataManager.h"

@implementation SLDownloadFileModel

- (SLDownloadState)downloadStateEnum {
    return self.downloadState.integerValue;
}

- (void)setDownloadStateEnum:(SLDownloadState)downloadStateEnum {
    self.downloadState = [NSNumber numberWithInteger:downloadStateEnum];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SLDownloadFileModelDownloadStateChanged" object:self];
}

- (void)setProgress:(NSNumber *)progress {
    _progress = progress;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SLDownloadFileModelDownloadProgressChanged" object:self];
}

- (NSString *)getDownloadStateDesc {
    NSString *downloadStateDesc = @"";
    switch ([self downloadStateEnum]) {
        case SLDownloadStatePaused:{
            downloadStateDesc = @"暂停";
            break;
        }
        case SLDownloadStateWait:{
            downloadStateDesc = @"等待下载";
            break;
        }
        case SLDownloadStateDownloading:{
            downloadStateDesc = @"下载中";
            break;
        }
        case SLDownloadStateFailed:{
            downloadStateDesc = @"下载失败";
            break;
        }
        case SLDownloadStateFinish:{
            downloadStateDesc = @"下载完成";
            break;
        }
    }
    
    return downloadStateDesc;
}

#pragma mark - Methods
- (void)saveData {
    [[SLDownloadDataManager shareInstance] saveDownloadFileModel:self];
}

- (CGRect)fileNameLabelFrame {
    CGFloat fileNameLabelMarginHorizontal = 10.0;
    CGFloat fileNameLabelMarginTop = 20.0;
    CGFloat fileNameLabelWidth = [UIScreen mainScreen].bounds.size.width - fileNameLabelMarginHorizontal * 2.0;
    CGFloat fileNameLabelHeight = 13.0;
    CGRect fileNameLabelFrame = CGRectMake(fileNameLabelMarginHorizontal, fileNameLabelMarginTop, fileNameLabelWidth, fileNameLabelHeight);
    
    return fileNameLabelFrame;
}

- (CGFloat)fileNameLabelMaxY {
    CGFloat fileNameLabelMaxY = self.fileNameLabelFrame.origin.y + self.fileNameLabelFrame.size.height;
    
    return fileNameLabelMaxY;
}

- (CGRect)downloadStateLabelFrame {
    CGFloat downloadStateLabelMarginLeft = 10.0;
    CGFloat downloadStateLabelMarginToFileNameLabel = 10.0;
    CGFloat downloadStateLabelWidth = ([UIScreen mainScreen].bounds.size.width - downloadStateLabelMarginLeft * 3.0) / 2.0;
    CGFloat downloadStateLabelHeight = 11.0;
    
    CGRect downloadStateLabelFrame = CGRectMake(downloadStateLabelMarginLeft, [self fileNameLabelMaxY] + downloadStateLabelMarginToFileNameLabel, downloadStateLabelWidth, downloadStateLabelHeight);
    return downloadStateLabelFrame;
}

- (CGFloat)downloadStateLebelMaxX {
    CGRect downloadStateLabelFrame = [self downloadStateLabelFrame];
    
    CGFloat downloadStateLebelMaxX = downloadStateLabelFrame.origin.x + downloadStateLabelFrame.size.width;
    return downloadStateLebelMaxX;
}

- (CGFloat)downloadStateLabelMaxY {
    CGRect downloadStateLabelFrame = [self downloadStateLabelFrame];
    
    CGFloat downloadStateLebelMaxY = downloadStateLabelFrame.origin.y + downloadStateLabelFrame.size.height;
    return downloadStateLebelMaxY;
}

- (CGRect)downloadProgressLabelFrame {
    CGRect downloadStateLabelFrame = [self downloadStateLabelFrame];
    CGFloat downloadProgressLabelMarginLeftToDownloadStateLabel = 10.0;
    CGFloat downloadProgressLabelLeft = [self downloadStateLebelMaxX] + downloadProgressLabelMarginLeftToDownloadStateLabel;
    CGFloat downloadProgressLabelTop = downloadStateLabelFrame.origin.y;
    CGFloat downloadProgressLabelWidth = downloadStateLabelFrame.size.width;
    CGFloat downloadProgressLabelHeight = downloadStateLabelFrame.size.height;
    
    CGRect downloadProgressLebelFrame = CGRectMake(downloadProgressLabelLeft, downloadProgressLabelTop, downloadProgressLabelWidth, downloadProgressLabelHeight);
    return downloadProgressLebelFrame;
}

- (CGFloat)cellHeight {
    CGFloat downloadStateLabelBottom = 20.0;
    
    CGFloat cellHeight = [self downloadStateLabelMaxY] + downloadStateLabelBottom;
    return cellHeight;
}

@end
